package cn.lysj.framework.common.utils.format;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @ Author ：drj.
 * @ Date  ：Created in 17:58 2019/2/25
 * @ Description:
 **/

public class FormatObject {

    public static Object format(Object object, int decimal) {
        Class clazz = object.getClass();
        //获取该实体类的所有属性
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            //设置属性可以访问
            field.setAccessible(true);
            if (BigDecimal.class.equals(field.getType())) {
                try {
                    //获取该属性的set方法
                    Method method = clazz.getDeclaredMethod("set" + upHeader(field.getName()), field.getType());
                    //获取属性值
                    BigDecimal val = (BigDecimal) field.get(object);
                    BigDecimal bigDecimal = BigDecimal.ZERO;
                    if (null != val) {
                        bigDecimal = new BigDecimal(String.valueOf(val)).setScale(decimal, BigDecimal.ROUND_HALF_DOWN);
                    }
                    //调用方法
                    method.invoke(object, bigDecimal);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (BigInteger.class.equals(field.getType())) {
                try {
                    //获取该属性的set方法
                    Method method = clazz.getDeclaredMethod("set" + upHeader(field.getName()), field.getType());
                    //获取属性值
                    BigInteger val = (BigInteger) field.get(object);
                    BigInteger bigInteger = BigInteger.ZERO;
                    if (null == val) {
                        method.invoke(object, bigInteger);
                    }
                    //调用方法
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (Long.class.equals(field.getType())) {
                try {
                    //获取该属性的set方法
                    Method method = clazz.getDeclaredMethod("set" + upHeader(field.getName()), field.getType());
                    //获取属性值
                    Long val = (Long) field.get(object);
                    if (null == val) {
                        val=0L;
                    }
                    //调用方法
                    method.invoke(object, val);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return object;
    }

    //首字母大写
    private static String upHeader(String input) {
        String head = input.substring(0, 1);
        String result = head.toUpperCase() + input.substring(1, input.length());
        return result;
    }


    public static BigDecimal formatZero(BigDecimal bigDecimal) {
        if (null == bigDecimal) {
            return BigDecimal.ZERO;
        }
        return bigDecimal;
    }

}
